#pragma once
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <cstring>
#include "Log.hpp"
std::string gpath = "./";
std::string gfilename = "Dictionary.txt";
std::string gseprator = ": ";
using namespace LogModule;
class Dictionary
{
    bool Split(std::string &s)
    {
        int pos = s.find(_seprator);
        if(pos==std::string::npos)  
            return false;
        std::string key = s.substr(0,pos);
        std::string val = s.substr(pos+_seprator.size());
        if(key.empty()||val.empty())
        {
            return false;
        }
        _dictionary[key] = val;
        return true;
    }
    bool LoadDictionary()
    {
        std::string file = _path + _filename;
        std::ifstream in(file);
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file " << file << "error";
            exit(1);
        }
        char inbuffer[64] = {0};
        while(in.getline(inbuffer,sizeof(inbuffer)))
        {
            std::string line = inbuffer;
            Split(line);
            memset(inbuffer, 0, sizeof(inbuffer));
        }
        in.close();
        return true;
    }

public:
    Dictionary(std::string &path = gpath, std::string &filename = gfilename,std::string& seprator = gseprator)
        : _path(path),
          _filename(filename),
          _seprator(seprator)
    {
        LoadDictionary();
    }
    std::string Translate(std::string& word)
    {
        std::unordered_map<std::string, std::string>::iterator it = _dictionary.find(word);
        if(it==_dictionary.end())
        {
            return "None";
        }
        return it->second;
    }
    ~Dictionary()
    {
        
    }

private:
    std::string _path;
    std::string _filename;
    std::string _seprator;
    std::unordered_map<std::string, std::string> _dictionary;
};